package com.demo.service.impl;


import com.demo.entity.enums.NoticeStatusEnum;
import com.demo.entity.enums.PageSize;
import com.demo.entity.enums.ResponseCodeEnum;
import com.demo.entity.po.NoticeInfo;
import com.demo.entity.po.SysAccount;
import com.demo.entity.query.NoticeInfoQuery;
import com.demo.entity.query.SimplePage;
import com.demo.entity.query.SysAccountQuery;
import com.demo.entity.vo.PaginationResultVO;
import com.demo.exception.BusinessException;
import com.demo.mappers.NoticeInfoMapper;
import com.demo.mappers.SysAccountMapper;
import com.demo.service.NoticeInfoService;
import com.demo.utils.CopyTools;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service("noticeInfoService")
public class NoticeInfoServiceImpl implements NoticeInfoService {

	@Resource
	private NoticeInfoMapper<NoticeInfo,NoticeInfoQuery> noticeInfoMapper;

	@Resource
	private SysAccountMapper<SysAccount, SysAccountQuery> userInfoMapper;

	/**
	 * 根据条件查询列表
	 */
	public List<NoticeInfo> findListByParam(NoticeInfoQuery query) { 
		return this.noticeInfoMapper.selectList(query);
	}

	/**
	 * 根据条件查询数量
	 */
	public Integer findCountByParam(NoticeInfoQuery query){
		return this.noticeInfoMapper.selectCount(query);
	}

	/**
	 * 分页查询
	 */
	public PaginationResultVO<NoticeInfo> findListByPage(NoticeInfoQuery  query) { 
		int count = this.findCountByParam(query);
		int pageSize = query.getPageSize()==null?PageSize.SIZE15.getSize():query.getPageSize();
		SimplePage page = new SimplePage(query.getPageNo(),count,pageSize);
		query.setSimplePage(page);
		List<NoticeInfo> list = this.findListByParam(query);
		return new PaginationResultVO(count,page.getPageSize(),page.getPageNo(),page.getPageTotal(),list);
	}

	/**
	 * 新增
	 */
	public Integer add(NoticeInfo bean) { 
		return this.noticeInfoMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	public Integer addBatch(List<NoticeInfo> listBean) { 
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.noticeInfoMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
	public Integer addOrUpdateBatch(List<NoticeInfo> listBean) { 
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.noticeInfoMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 根据Id查询
	 */
	public NoticeInfo getNoticeInfoById(Integer id) { 
		return this.noticeInfoMapper.selectById(id);
	}

	/**
	 * 根据Id更新
	 */
	public Integer updateNoticeInfoById(NoticeInfo bean, Integer id) { 
		return this.noticeInfoMapper.updateById(bean,id);
	}


	/**
	 * 根据Id删除
	 */
	public Integer deleteNoticeInfoById(Integer id,Integer userId) {
		NoticeInfo dbNotice = this.noticeInfoMapper.selectById(id);
		if (dbNotice==null||!dbNotice.getUserId().equals(userId)){
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		return this.noticeInfoMapper.deleteById(id);
	}

	/**
	 * 保存通知信息
	 * @param bean
	 */
	@Override
	public void saveNotice(NoticeInfo bean) {
		Date curDate=new Date();
		if (bean.getId()!=null){//保存
			bean.setUpdateTime(curDate);
			this.noticeInfoMapper.updateById(bean,bean.getId());
		}else {
			NoticeInfo noticeInfo = CopyTools.copy(bean,NoticeInfo.class);
			SysAccount userInfo = this.userInfoMapper.selectByUserId(noticeInfo.getUserId());
			noticeInfo.setEmail(userInfo.getEmail());
			noticeInfo.setName(userInfo.getUserName());
			noticeInfo.setUpdateTime(curDate);
			noticeInfo.setCreateTime(curDate);
			noticeInfo.setPhone(userInfo.getPhone()==null?"":userInfo.getPhone());
			noticeInfo.setStatus(NoticeStatusEnum.UNDER_PUBLISH.getStatus());
			this.noticeInfoMapper.insert(noticeInfo);
		}
	}

	/**
	 * 修改通知状态
	 * @param id 通知id
	 * @param status 状态
	 */
	@Override
	public void updateStatus(Integer id, Integer status,Integer userId) {
		//验证id
		NoticeInfo dbNoticeInfo = this.noticeInfoMapper.selectById(id);
		if (null==dbNoticeInfo){
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		//验证status
		NoticeStatusEnum statusEnum = NoticeStatusEnum.getByStatus(status);
		if (statusEnum==null){
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		//不允许修改其他管理员的通知
		if (!dbNoticeInfo.getUserId().equals(userId)){
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}

		NoticeInfo updateInfo = new NoticeInfo();
		Date curDate = new Date();
		if (NoticeStatusEnum.PUBLISHED.getStatus().equals(status)){
		updateInfo.setPublishTime(curDate);
		}
		updateInfo.setUpdateTime(curDate);
		updateInfo.setStatus(status);
		this.noticeInfoMapper.updateById(updateInfo,id);
	}
}